package com.sias.spring.mybatis;

import com.sias.sogra.mapper.OrderMapper;
import com.sias.sogra.mapper.UserMapper;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;
import org.springframework.util.MultiValueMap;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author 吴文杰
 * @version 1.0
 * @createTime 2023-05-30 15:35:33
 */
public class SograImportBeanDefinitionRegister implements ImportBeanDefinitionRegistrar {
  @Override
  public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator) {

    Map<String, Object> annotationAttributes = importingClassMetadata.getAnnotationAttributes(MapperScan.class.getName());
    String path = (String) annotationAttributes.get("value");

    SograScanner sograScanner = new SograScanner(registry);
    sograScanner.addIncludeFilter(new TypeFilter() {
      @Override
      public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
        return true;
      }
    });
    sograScanner.scan(path);


    // AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
    // beanDefinition.setBeanClass(SograFactoryBean.class);
    // beanDefinition.getConstructorArgumentValues().addGenericArgumentValue(UserMapper.class);
    // registry.registerBeanDefinition("userMapper",beanDefinition);
    //
    // AbstractBeanDefinition beanDefinition1 = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
    // beanDefinition1.setBeanClass(SograFactoryBean.class);
    // beanDefinition1.getConstructorArgumentValues().addGenericArgumentValue(OrderMapper.class);
    // registry.registerBeanDefinition("orderMapper",beanDefinition1);
  }
}
